package com.jvtd.eyl.app;

import com.jvtd.app.JvtdApplication;
import com.jvtd.eyl.BuildConfig;
import com.jvtd.eyl.constants.App;
import com.jvtd.eyl.data.DbManager;
import com.jvtd.eyl.di.component.ApplicationComponent;
import com.jvtd.eyl.di.component.DaggerApplicationComponent;
import com.jvtd.eyl.di.module.ApplicationModule;
import com.jvtd.eyl.di.module.NetModule;
import com.jvtd.eyl.utils.filedownload.FileDownloaderHelper;
import com.liulishuo.filedownloader.FileDownloader;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.crashreport.CrashReport;

import org.litepal.LitePal;

import javax.inject.Inject;

public class MyApplication extends JvtdApplication {
    @Inject
    DbManager mDbManager;
    private ApplicationComponent mApplicationComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        mApplicationComponent = DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule())
                .netModule(new NetModule(App.HTTP_BASE_URL, this))
                .build();

        mApplicationComponent.inject(this);

        //配置开发工具
        configDevelopment();
    }

    /**
     * 全局初始化，onCreate自动调用
     */
    @Override
    public void configGlobalInit() {
        initDatabase();
        initFileDownload();
    }

    /**
     * 初始化数据库
     */
    private void initDatabase() {
        LitePal.initialize(this);
    }

    /**
     * 初始化文件下载
     */
    private void initFileDownload() {
        FileDownloaderHelper.initInDownloadProcess(this);
    }

    public ApplicationComponent getApplicationComponent()
    {
        return mApplicationComponent;
    }


    // TODO: 2018/9/25 开发使用 发布注释
    @Override
    public void configDevelopment() {
        super.configDevelopment();
        initBugly();
//        if (!LeakCanary.isInAnalyzerProcess(this))
//          LeakCanary.install(this);
    }

    private void initBugly() {
//         TODO: 2018/3/20 设置开发设备 测试时使用 发布后注释
        CrashReport.setIsDevelopmentDevice(getApplicationContext(), BuildConfig.DEBUG);
        // 初始化Bugly
        // TODO: 2017/11/23 发布后将true置为false
        Bugly.init(getApplicationContext(), App.BUGLY_APP_ID, true);
    }
}
